/**
 * 
 */
package com.ccqlk.attendance.repository.service.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ccqlk.attendance.common.page.PageRequest;
import com.ccqlk.attendance.common.page.Sort;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.common.BaseMapper;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: BaseServiceImpl<br>
 * 文件说明: TODO<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月27日 上午9:47:56<br>
 */
public class BaseServiceImpl<T, ID> implements BaseService<T, ID> {

	private BaseMapper<T> baseMapper;
	
	/**
	 * 
	 */
	public BaseServiceImpl(BaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
	}
	
	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#add(java.lang.Object)
	 */
	@Override
	public int add(T entity) {
		return baseMapper.insertSelective(entity);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#edit(java.lang.Object)
	 */
	@Override
	public int edit(T entity) {
		return baseMapper.updateByPrimaryKeySelective(entity);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#delete(java.lang.Object)
	 */
	@Override
	public int delete(ID key) {
		return baseMapper.deleteByPrimaryKey(key);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#selectByPrimaryKey(java.lang.Object)
	 */
	@Override
	public T selectByPrimaryKey(ID key) {
		return baseMapper.selectByPrimaryKey(key);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#findOne(java.lang.Object)
	 */
	@Override
	public T selectOne(ID key) {
		return baseMapper.selectByPrimaryKey(key);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#selectAll()
	 */
	@Override
	public List<T> selectAll() {
		return baseMapper.selectAll();
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#selectAll(com.ccqlk.attendance.common.page.Sort)
	 */
	@Override
	public List<T> selectAll(Sort sort) {
		setSortInfo(sort);
		return baseMapper.selectAll();
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.repository.service.BaseService#selectAll(com.ccqlk.attendance.common.page.PageRequest)
	 */
	@Override
	public PageInfo<T> selectAll(PageRequest pageRequest) {
		setPageInfo(pageRequest);
		return new PageInfo<>(selectAll());
	}
	
	protected void setSortInfo(Sort sort) {
		if (sort!=null) {
			PageHelper.orderBy(sort.getColumn()+" "+sort.getDirection().getName());
		}
	}
	
	protected void setPageInfo(PageRequest pageRequest) {
		if (pageRequest!=null) {
			PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize(), pageRequest.getCountable());
			
			Sort sort = pageRequest.getSort();
			String orderBy = null;
			if (sort!=null) {
				orderBy = sort.getColumn()+" "+sort.getDirection().getName();
			}
			
			List<Sort> sorts = pageRequest.getSortList();
			if (sorts!=null && sorts.size()>0) {
				StringBuilder stringBuilder = new StringBuilder();
				for (int index = 0, length = sorts.size(); index < length; index++) {
					Sort temp = sorts.get(index);
					if (temp != null) {
						stringBuilder.append(temp.getColumn());
						stringBuilder.append(" ");
						stringBuilder.append(temp.getDirection().getName());
					}
					
					if (index < length-1) {
						stringBuilder.append(",");
					}
				}
				orderBy = stringBuilder.toString();
			}
			
			if (orderBy!=null && StringUtils.isNotEmpty(orderBy)) {
				PageHelper.orderBy(orderBy);
			}
			
		}
	}

}
